package top.hyz.hrm.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import top.hyz.hrm.AssertUtils;
import top.hyz.hrm.ErrorCode;
import top.hyz.hrm.constant.CourseConstant;
import top.hyz.hrm.domain.CourseDetail;
import top.hyz.hrm.domain.CourseType;
import top.hyz.hrm.mapper.CourseTypeMapper;
import top.hyz.hrm.service.ICourseTypeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.hyz.hrm.vo.CrumbsVO;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 课程目录 服务实现类
 * </p>
 *
 * @author Hanyuze
 * @since 2021-01-19
 */
@Service
@Slf4j
public class CourseTypeServiceImpl extends ServiceImpl<CourseTypeMapper, CourseType> implements ICourseTypeService {

    @Override
    @Cacheable(cacheNames = CourseConstant.COURSE_TYPE_CACHE_KEY, key = " 'ALL' ")
    public List<CourseType> treeData() {
        return findAll();
    }

    /**
     * 树形菜单
     *
     * @return 课程分类菜单集合
     */
    private List<CourseType> findAll() {
        System.out.println("未走缓存");
        //查询所有课程分类
        List<CourseType> courseTypes = baseMapper.selectList(null);
        //准备菜单集合
        ArrayList<CourseType> firstMenu = new ArrayList<>(10);
        //遍历所有课程分类
        for (CourseType courseType : courseTypes) {
            //将parentId为0的放入firstMenu中
            if (courseType.getPid() == 0) {
                firstMenu.add(courseType);
            } else {
                //嵌套for循环，将parentId不为空，且parentId等于parent.Id的courseType放入firstMenu中对象的children属性中
                for (CourseType parent : courseTypes) {
                    Long pid = courseType.getPid();
                    if (pid != null && pid.equals(parent.getId())) {
                        parent.getChildren().add(courseType);
                    }
                }
            }
        }
        return firstMenu;
    }

    @Override
    @CacheEvict(cacheNames = CourseConstant.COURSE_TYPE_CACHE_KEY, key = " 'ALL' ")
    public boolean insert(CourseType entity) {
        return super.insert(entity);
    }

    @Override
    @CacheEvict(cacheNames = CourseConstant.COURSE_TYPE_CACHE_KEY, key = " 'ALL' ")
    public boolean deleteById(Serializable id) {
        return super.deleteById(id);
    }

    @Override
    @CacheEvict(cacheNames = CourseConstant.COURSE_TYPE_CACHE_KEY, key = " 'ALL' ")
    public boolean updateById(CourseType entity) {
        return super.updateById(entity);
    }

    /**
     * 面包屑菜单
     *
     * @param courseTypeId 课程分类id
     * @return 面包屑菜单集合
     */
    @Override
    public List<CrumbsVO> crumbs(Long courseTypeId) {
        //判断courseTypeId非空
        AssertUtils.isNotEmpty(courseTypeId, ErrorCode.COURSE_TYPE_ID_NOT_EMPTY_501);
        //根据courseTypeId查询分类信息
        CourseType courseType = baseMapper.selectById(courseTypeId);
        //判断查询结果非空
        AssertUtils.isNotEmpty(courseType, ErrorCode.COURSE_NOT_EMPTY_502);
        //拿到path  1043.1044.1050
        String path = courseType.getPath();
        String[] courseTypePaths = path.split("\\.");

        ArrayList<CrumbsVO> result = new ArrayList<>();
        //循环查询
        for (String pathId : courseTypePaths) {
            CrumbsVO crumbsVO = new CrumbsVO();
            CourseType self = baseMapper.selectById(pathId);
            crumbsVO.setSelf(self);
            List<CourseType> brothers = baseMapper.SelectBrotherByPid(self.getPid());
            crumbsVO.setBrother(brothers);
            result.add(crumbsVO);
        }
        return result;
    }
}
